terraform {
  required_providers {
    yandex = {
      source = "yandex-cloud/yandex"
    }
  }
  required_version = ">= 0.13"
}

provider "yandex" {
  zone = "ru-central1-b"
  folder_id = ""   --- REPLACE WITH YOUR FOLDER
  service_account_key_file = file("key.json")
}

resource "yandex_compute_disk" "debian-postgre-disk" { 
       name = "debian-postgre-disk" 
       type = "network-hdd" 
       zone = "ru-central1-b"
       size = 30 
  }

resource "yandex_compute_instance" "debian-pg-1" {
  name = "debian-postgres"
  hostname = "db-1"

  resources {
    cores  = 4
    memory = 12
  }

  boot_disk {
    initialize_params {
      image_id="fd88cgdnirrp00ujt772"
      size = "30"
    }
  }
  
  secondary_disk { 
          disk_id = yandex_compute_disk.debian-postgre-disk.id 
  }

  network_interface {
    subnet_id = data.yandex_vpc_subnet.subnet-1.id
    nat       = true
  }

  metadata = {
    ssh-keys = "debian:${file("C://Users//User//.ssh//SOME_YOUR_PUB_KEY.pub")}"
  }
}

data   "yandex_vpc_network" "network-1" {
  name = "network1"
}

data "yandex_vpc_subnet" "subnet-1" {
  name           = "subnet1b"
}

output "internal_ip_address_vm_1" {
  value = yandex_compute_instance.debian-pg-1.network_interface.0.ip_address
}

data "template_file" "myppk" { template = "${file("C://Users//User//.ssh//SOME_YOUR_PUB_KEY.pub")}" }

resource "null_resource" "rexec_all-deb-01" {

  provisioner "remote-exec" {
    inline = [
      "sleep 5",
      "echo ----------------------------------------------------------------------------------------------",
      "echo ==================================START root section=========================================",
      "echo ----------------------------------------------------------------------------------------------",
      "pwd",
      "sudo apt-get update && sudo apt-get install -y pkgconf vim systemtap-sdt-dev bpftrace meson ninja-build git build-essential libreadline-dev zlib1g-dev flex bison libxml2-dev libxslt-dev libssl-dev libxml2-utils xsltproc ccache sudo",
      "sudo sed -i 's/^%sudo.*ALL=(ALL:ALL) ALL/%sudo ALL=(ALL:ALL) NOPASSWD:ALL/' /etc/sudoers",
      "sudo useradd -rm -d /home/postgres -G sudo -s /bin/bash postgres",

      "echo ==================================STARR Postgres user install=========================================",
      "sudo mkdir /home/postgres/.ssh && sudo chown postgres /home/postgres/.ssh/",
      "sudo bash -c \"echo '${file("C://Users//User//.ssh//SOME_YOUR_PUB_KEY.pub")}' >> /home/postgres/.ssh/authorized_keys\"",
      "echo ==================================OPtional systemtap=========================================",
      "sudo apt-get install -y systemtap linux-headers-$(uname -r)"
    ]
    connection {
      host = "${yandex_compute_instance.debian-pg-1.network_interface.0.nat_ip_address}"
      type = "ssh"
      user = "debian"
      private_key = "${file("C://Users//User//.ssh//SOME_YOUR_PRIV_KEY.ppk")}"
    }
  }
  provisioner "remote-exec" {
    inline = [
      "sleep 2",
      "echo ----------------------------------------------------------------------------------------------",
      "echo ==================================START postgres section=========================================",
      "echo ----------------------------------------------------------------------------------------------",
      "echo start configure postgres",
      "pwd && whoami",
      "git clone -b REL_15_STABLE https://github.com/postgres/postgres.git -q",
      "git clone https://github.com/FritsHoogland/postgres-bpftrace.git",
      "cp /home/postgres/postgres-bpftrace/docker/wait_event.patch /home/postgres/postgres/",
      "echo ==================================apply patch =========================================",
      "cd /home/postgres/postgres && git apply wait_event.patch",
      "sleep 10",
      "cd  /home/postgres/postgres",
      "echo ==================================configure =========================================",
      "./configure --prefix=/usr/local/pgsql",
      "echo ==================================make =========================================",
      "make -j 4 1>/dev/null", 
      "echo ==================================make install =========================================",
      "sudo make install 1>/dev/null",
      "sudo mkdir -p /usr/local/pgsql/data && sudo chown postgres /usr/local/pgsql/data",
      "/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data",
      "cd ",
      "echo \"export PATH=$PATH:/usr/local/pgsql/bin\" >> .bashrc",
      "echo ----------------------------------------------------------------------------------------------",
      "echo ==================================START configure section=========================================",
      "echo ----------------------------------------------------------------------------------------------",
      "cd ",
      "echo \"export PGDATA=/usr/local/pgsql/data\" >> .bashrc",
      "cd ~/postgres-bpftrace/ && sed -i 's#/usr/lib/postgresql/15/#/usr/local/pgsql/#' ~/postgres-bpftrace/query-analyzer.bt"
    ]
    connection {
      host = "${yandex_compute_instance.debian-pg-1.network_interface.0.nat_ip_address}"
      type = "ssh"
      user = "postgres"
      private_key = "${file("C://Users//User//.ssh//SOME_YOUR_PRIV_KEY.ppk")}"
    }
  }

  depends_on = [yandex_compute_instance.debian-pg-1]
}

resource "null_resource" "rexec_all-deb-002" {

  provisioner "remote-exec" {
    inline = [
      "sleep 2",
      "echo ----------------------------------------------------------------------------------------------",
      "echo ==================================START configure section=========================================",
      "echo ----------------------------------------------------------------------------------------------",
      "cd ",
      "cd postgres-bpftrace/ && sed -i 's#/usr/lib/postgresql/15/#/usr/local/pgsql/#' query-analyzer.bt",
    ]
    connection {
      host = "${yandex_compute_instance.debian-pg-1.network_interface.0.nat_ip_address}"
      type = "ssh"
      user = "postgres"
      private_key = "${file("C://Users//User//.ssh//SOME_YOUR_PRIV_KEY.ppk")}"
    }
  }

  depends_on = [null_resource.rexec_all-deb-01]
}

